﻿using System.Collections.Generic;
using System.Drawing;
using NeuronDotNet.Samples.OCR.layoutsstategies;
using NeuronDotNet.Controls;

namespace NeuronDotNet.Samples.OCR
{
  /// <summary>
  /// This class is used for creating a list of letters, 
  /// using the base drawing and an algorithm for determining
  /// the division between different letters.
  /// </summary>
  internal class ImageLayout
  {
    #region private fields
    /// <summary>
    /// Current Letter.
    /// </summary>
    private readonly Letter image;
    /// <summary>
    /// Implements the GetDivision() method, which returns a list
    /// of rectangles that divide the letters.
    /// </summary>
    private readonly ILayoutStrategy layoutStrategy;
    #endregion

    #region constructor
    /// <summary>
    /// Set image and layoutStrategy.
    /// </summary>
    public ImageLayout(ILayoutStrategy layoutStrategy)
    {
      this.layoutStrategy = layoutStrategy;
      image = layoutStrategy.Letter;
    }
    #endregion

    #region public methods
    /// <summary>
    /// Calls the private method GetClippedLetters()
    /// with the current letter Division.
    /// </summary>
    public List<Letter> GetClippedLetters()
    {
      List<Rectangle> division = layoutStrategy.GetDivision();
      return GetClippedLetters(division);
    }
    #endregion

    #region private method
    /// <summary>
    /// Returns a list of clipped Letters, according to a given division.
    /// </summary>
    private List<Letter> GetClippedLetters(IEnumerable<Rectangle> rectangles)
    {
      var result = new List<Letter>();

      foreach (Rectangle rectangle in rectangles)
      {
        Letter clipedImage = image.MakeCliped(rectangle);
        result.Add(clipedImage);
      }

      return result;
    }
    #endregion
  }
}
